package com.compty.paint.web.front;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.compty.core.framework.util.StringUtil;
import com.compty.core.page.Page;
import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.Advertisement;
import com.compty.paint.domain.Painting;
import com.compty.paint.manager.AdvertisementManager;
import com.compty.paint.manager.PaintingManager;

/**
 * @Todo:绘画作品Action
 * @CreateTime:2011-6-3 上午10:55:51
 */
public class PaintingAction extends BaseAction<Painting> {

	private static final long serialVersionUID = -7191388580534005001L;
	
    private PaintingManager paintingManager;
    private AdvertisementManager advertisementManager;
    private List<Advertisement> advertisementList; /*** 广告集合 ****/
    
	public PaintingAction() {
		super();
		super.pageSize = 12;
	}
	
	/**
	* @Description: 分页请求画家作品(根据作品类型(作品大类型如瓷器、艺术家具)查找)
	 */
	@SuppressWarnings("unchecked")
	public String pList(){
		
		String paint_type = request.getParameter("type");
		String[] types = judgePaintingTypeAndAdvertiseType(paint_type);

        Page p = advertisementManager.findByPage(1,3,types[1]);//频道广告
        this.advertisementList = (List<Advertisement>) p.getResult();
        
		String pageNo = request.getParameter("pageNo");
		Integer currentPage = StringUtil.isDigit(pageNo)? Integer.valueOf(pageNo) : 1;
        this.page = paintingManager.findAvailablePaintingsByPage(currentPage, 12, types[0]);
        
		return "painting_list";
	}
	
	/**
	* @Description: 无刷新分页请求画家作品(根据作品类型查找)
	 */
	@SuppressWarnings("unchecked")
	public String query_ajax2(){
		String paint_type = request.getParameter("type");
		String[] types = judgePaintingTypeAndAdvertiseType(paint_type);
		
		String pageNo = request.getParameter("pageNo");
		Integer currentPage = StringUtil.isDigit(pageNo)? Integer.valueOf(pageNo) : 1;
        Page page = paintingManager.findAvailablePaintingsByPage(currentPage, 12, types[0]);
        
        Object result = page.getResult();
        StringBuffer arrayBuffer = new StringBuffer("{array:[");
        if( result != null){
        	List list = (List)result;
        	for (int i = 0; i < list.size(); i++) {
				Painting p = (Painting) list.get(i);
				String middleImageApath = p.getMiddleImageApath();
				String painterName = p.getPainter() != null ? p.getPainter().getName():"";
				String title = p.getTitle();
				Long id = p.getId();
				arrayBuffer.append("{id:").append(id).append(",title:\"").append(title).append("\",painterName:\"").append(painterName).append("\",middleImageApath:\"").append(middleImageApath).append("\"");
				arrayBuffer.append(",qqxl:").append(p.getQqxl()).append(",unit:\"").append(p.getUnit()).append("\",onSaleFlag:\"").append(p.getOnSaleFlag()).append("\",bidPrice:").append(p.getBidPrice()).append(",price:").append(p.getPrice());
				arrayBuffer.append(",store:").append(p.getStore()).append(",remark:\"").append(p.getRemark()).append("\"");
				arrayBuffer.append("}");
				if( i < list.size()-1){
					arrayBuffer.append(",");
				}
			}
        }
        arrayBuffer.append("],page:{pageNo:").append(page.getPageNo()).append(",pageCount:").append(page.getPageCount()).append("}");
        arrayBuffer.append("}");
        
        request.setAttribute("ajaxData", arrayBuffer.toString());
        
		return "query_ajax";
	}
	
	/**
	* @Description: 分页请求画家作品(根据作品类型(作品大类型如瓷器、艺术家具)查找),以画家为单位分画集显示
	 */
	@SuppressWarnings("unchecked")
	public String pList_gallery(){
		
		String paint_type = request.getParameter("type");
		String[] types = judgePaintingTypeAndAdvertiseType(paint_type);

        Page p = advertisementManager.findByPage(1,3,types[1]);//频道广告
        this.advertisementList = (List<Advertisement>) p.getResult();
        
		String pageNo = request.getParameter("pageNo");
		Integer currentPage = StringUtil.isDigit(pageNo)? Integer.valueOf(pageNo) : 1;
        this.page = paintingManager.findAvailablePaintingsSortOfPainterByPage(currentPage, 12, types[0]);
        
		return "painting_list_gallery";
	}
	
	/**
	* @Description: 无刷新分页请求画家作品(根据作品类型(作品大类型如瓷器、艺术家具)查找),以画家为单位分画集显示
	 */
	@SuppressWarnings("unchecked")
	public String query_ajax_gallery(){
		String paint_type = request.getParameter("type");
		String[] types = judgePaintingTypeAndAdvertiseType(paint_type);
		
		String pageNo = request.getParameter("pageNo");
		Integer currentPage = StringUtil.isDigit(pageNo)? Integer.valueOf(pageNo) : 1;
        Page page = paintingManager.findAvailablePaintingsSortOfPainterByPage(currentPage, 12, types[0]);
        
        Object result = page.getResult();
        StringBuffer arrayBuffer = new StringBuffer("{array:[");
        if( result != null){
        	List list = (List)result;
        	for (int i = 0; i < list.size(); i++) {
				Painting p = (Painting) list.get(i);
				String middleImageApath = p.getMiddleImageApath();
				String painterName = p.getPainter() != null ? p.getPainter().getName():"";
				String englishName = p.getPainter() != null ? p.getPainter().getEnglishName():"";
				String catalogType_name = p.getPainter() != null ? p.getPainter().getCatalogType().getName():"";
				String honour = p.getPainter() != null ? p.getPainter().getHonour():"";
				String graduatedFrom = p.getPainter() != null ? p.getPainter().getGraduatedFrom():"";
				String painterImagepath = p.getPainter() != null ? p.getPainter().getOriginalImagePath():"";
				String title = p.getTitle();
				Long id = p.getId();
				arrayBuffer.append("{id:").append(id).append(",title:\"").append(title).append("\",painterName:\"").append(painterName).append("\",middleImageApath:\"").append(middleImageApath).append("\"");
				arrayBuffer.append(",qqxl:").append(p.getQqxl()).append(",unit:\"").append(p.getUnit()).append("\",onSaleFlag:\"").append(p.getOnSaleFlag()).append("\",bidPrice:").append(p.getBidPrice()).append(",price:").append(p.getPrice());
				arrayBuffer.append(",store:").append(p.getStore()).append(",remark:\"").append(p.getRemark()).append("\",englishName:\"").append(englishName);
				arrayBuffer.append("\",catalogType_name:\"").append(catalogType_name).append("\",honour:\"").append(honour).append("\",painterImagepath:\"").append(painterImagepath);
				arrayBuffer.append("\",graduatedFrom:\"").append(graduatedFrom);
				arrayBuffer.append("\"}");
				if( i < list.size()-1){
					arrayBuffer.append(",");
				}
			}
        }
        arrayBuffer.append("],page:{pageNo:").append(page.getPageNo()).append(",pageCount:").append(page.getPageCount()).append("}");
        arrayBuffer.append("}");
        
        request.setAttribute("ajaxData", arrayBuffer.toString());
        
		return "query_ajax";
	}
	

	/**
	* @Description: 无刷新分页请求名家作品(根据作品类型(国画、油画)查找)
	 */
	@SuppressWarnings("unchecked")
	public String query_ajax(){
		String paint_type = request.getParameter("type");
		if(!StringUtil.isDigit(paint_type)){
			paint_type = "1";
		}
		
		String pageNo = request.getParameter("pageNo");
		Integer currentPage = StringUtil.isDigit(pageNo)? Integer.valueOf(pageNo) : 1;
        Page page = paintingManager.findPaintingsByPageOfPainterStarByRandom(paint_type,currentPage,12);
        
        Object result = page.getResult();
        StringBuffer arrayBuffer = new StringBuffer("{array:[");
        if( result != null){
        	List list = (List)result;
        	for (int i = 0; i < list.size(); i++) {
				Painting p = (Painting) list.get(i);
				String middleImageApath = p.getMiddleImageApath();
				String painterName = p.getPainter() != null ? p.getPainter().getName():"";
				String title = p.getTitle();
				Long id = p.getId();
				arrayBuffer.append("{id:").append(id).append(",title:\"").append(title).append("\",painterName:\"").append(painterName).append("\",middleImageApath:\"").append(middleImageApath).append("\"}");
				if( i < list.size()-1){
					arrayBuffer.append(",");
				}
			}
        }
        arrayBuffer.append("],page:{pageNo:").append(page.getPageNo()).append(",pageCount:").append(page.getPageCount()).append("}");
        arrayBuffer.append("}");
        
        request.setAttribute("ajaxData", arrayBuffer.toString());
        
		return "query_ajax";
	}
	
	/**绘画频道主页**/
	@SuppressWarnings("unchecked")
	public String index(){
		
        this.page = paintingManager.findAvailablePaintingsSortOfPainterByPage(curPage,pageSize,null);
        
        Page p = advertisementManager.findByPage(1,3,"3");//当代画家频道广告
        this.advertisementList = (List<Advertisement>) p.getResult();
		
        return "listAll";
	}
    
    /**
     *@Todo:根据画家查找其所有作品
     *@CreateTime:2011-9-6 上午09:58:12
     */
    @SuppressWarnings("unchecked")
	public String listPaintingsByPainter(){
    	
    	String  painterIdStr = request.getParameter("id");
    	if( !StringUtil.isDigit(painterIdStr) ){
    		return ERROR;
    	}
    		
    	Long painterId =  Long.valueOf(painterIdStr);
    	List<Painting>  paintingList = paintingManager.findAvailablePaintingsByPainter(painterId);
    	
		List<Map> returnList = new ArrayList<Map>();
		for (Painting painting : paintingList) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", painting.getId());
			map.put("title", painting.getTitle());
			map.put("painterId", painting.getPainter().getId());
			map.put("painter_name", painting.getPainter().getName());
			map.put("imageid", painting.getImageInfo().getId());
			map.put("suffix", painting.getImageInfo().getSuffix());
			map.put("originalImagePath", painting.getOriginalImagePath());
			map.put("smallImagePath", painting.getSmallImagePath());
			if(painting.getEntryPerson() != null){
				map.put("provider", painting.getEntryPerson().getUsername());
			}else{
				map.put("provider", "");
			}
			returnList.add(map);
    		
    		String  ajaxData = JSONArray.fromObject(returnList).toString();
    		request.setAttribute("ajaxData", ajaxData);
    	}

    	return "listPaintingsByPainter";
    }
    
    /**
     * arr[0]作品类型,arr[1]广告类型
     */
    private String[] judgePaintingTypeAndAdvertiseType(String paint_type){
    	String[] types = new String[2];
    	
    	if(paint_type == null){
    		types[0] =  null;
			types[1] = "3";
			return types;
    	}
    	
		if(!StringUtil.isDigit(paint_type)){
			types[0] =  null;
			types[1] = "1";
			return types;
		}
		
		if(paint_type.equals("1")|| paint_type.equals("2")){
			types[0] =  paint_type;
			types[1] = "3";
		}else if(paint_type.equals("3")){
			types[0] =  "3";
			types[1] = "2";
		}else{
			types[0] =  paint_type;
			types[1] = paint_type;
		}
    	return types;
    }


	public void setPaintingManager(PaintingManager paintingManager) {
		this.paintingManager = paintingManager;
	}

	public void setAdvertisementManager(AdvertisementManager advertisementManager) {
		this.advertisementManager = advertisementManager;
	}
	
	public List<Advertisement> getAdvertisementList() {
		return advertisementList;
	}

	public void setAdvertisementList(List<Advertisement> advertisementList) {
		this.advertisementList = advertisementList;
	}

}
